﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PortChat
{
    //for correcting errors in input string
    class ErrorCorrector
    {
        //corrects errors in input string
        //public String correctError(String strError, Encoder encoder)
        //{
        //    //list of all code words
        //    List<String> lstStrCodeWords = encoder.getCodeWords();
        //    //input word with errors
        //    char[] charArrError = strError.ToCharArray();
        //    //the current code word which is going to compared
        //    char[] charArrCodeWord;
        //    //current position in word which is going to compared
        //    int position = 0;
        //    //saves the highest matches between charArrError and charArrCodeWord
        //    int maxMatchCount = 0;
        //    //saves the current matches betwenn charArrError and charArrCodeWord 
        //    int matchCount = 0;
        //    //code word with the highest number of matches of digits
        //    char[] maxMatchCodeWord;
        //    //current code word which is going to compared
        //    //char[] matchCodeWord;
        //    //return string
        //    String maxCodeWord;
        //    //for all code words do
        //    maxCodeWord = "";
        //    maxMatchCount = 0;
        //    maxMatchCodeWord = strError.ToCharArray();

        //    foreach (String myCodeWord in lstStrCodeWords)
        //    {
        //        charArrCodeWord = myCodeWord.ToCharArray();
        //        //compute number of matches between charArrError and charArrCodeWord
        //        position = 0;
        //        //for all digits of the code word do
        //        foreach (char myCharArrCodeWord in charArrCodeWord)
        //        {
        //            if(myCharArrCodeWord.Equals(charArrError[position]))
        //            {
        //                //matched digits
        //                matchCount++;
        //            }
        //            position++;
                    
        //        }
        //        if (matchCount > maxMatchCount)
        //        {
        //            maxMatchCount = matchCount;
        //            maxMatchCodeWord = charArrCodeWord;
        //        }
        //        matchCount = 0;
        //    }
        //    //convert char array to string
        //    foreach (char myChar in maxMatchCodeWord)
        //    {
        //        maxCodeWord = maxCodeWord + myChar;
        //    }
        //    //Console.WriteLine(maxCodeWord);
        //    return maxCodeWord;
        //}

        //corrects codeword with error
        public String correctCodeWord(String strCodeWordWithErrorP, int positionOfErrorP)
        {
            char[] charArrCodeWordWithError = strCodeWordWithErrorP.ToCharArray();

            //invert the digit which has an error
            if (charArrCodeWordWithError[positionOfErrorP].Equals('0'))
            {
                charArrCodeWordWithError[positionOfErrorP] = '1';
            }
            else
            {
                charArrCodeWordWithError[positionOfErrorP] = '0';
            }

            String strCodeWordCorrected = "";
            foreach (char myCodeWord in charArrCodeWordWithError)
            {
                strCodeWordCorrected = strCodeWordCorrected + myCodeWord;
            }
            return strCodeWordCorrected;
        }
    }
}
